Basic
The TreeView allows exploring of hierarchic data. In its simplest form it only displays text via the Text
property of its items.
But you can also attach a value of type T
to each item via the Value
property.
In this example ReadOnly
is set to true to prevent value selection. Some items in this example have a text, some have a value and one has both. If you use
only Text
and T="string"
then that text will also serve as value. If you set only Value
then the text will be derived from the value. You can of course set both to have different Text
and Value
. This will become important
for value selection (see Selection).
Getting Started
Installation
Components
Avatar
Button
<MudTreeView T="string" ReadOnly> <MudTreeViewItem Text="Getting Started"> <MudTreeViewItem Text="Installation" /> </MudTreeViewItem> <MudTreeViewItem Value='"Components"'> <MudTreeViewItem Text="Avatar" Value='"MudAvatar"' /> <MudTreeViewItem Text="Button" Value='"MudButton"' /> </MudTreeViewItem> </MudTreeView>
Usage
Hover
applies a hover effect on mouse-over. Ripple
applies a ripple effect on click, except if
ExpandOnDoubleClick
is set. Dense
will result in a more compact vertical padding of the item items to save space.
Disabled
will prevent all interaction with any items.
With ExpandOnClick
a subtree can be expanded and collapsed by clicking on it. With ExpandOnDoubleClick
,
only a double-click will expand or collapse the subtrees. Additionally, a OnDoubleClick
callback can be assigned to set a custom double click behaviour.
Applications
Terminal
Documents
MudBlazor
API
Components
Features
<MudPaper Width="300px" Elevation="0"> <MudTreeView T="string" ReadOnly="" Hover="" Dense="" Disabled="" ExpandOnClick="" ExpandOnDoubleClick=""> <MudTreeViewItem Text="Applications" Expanded> <MudTreeViewItem Text="Terminal" /> </MudTreeViewItem> <MudTreeViewItem Text="Documents" Expanded> <MudTreeViewItem Text="MudBlazor" Expanded> <MudTreeViewItem Text="API" /> <MudTreeViewItem Text="Components" /> <MudTreeViewItem Text="Features" /> </MudTreeViewItem> </MudTreeViewItem> </MudTreeView> </MudPaper> <MudStack Row Wrap="Wrap.Wrap" Justify="Justify.Center"> <MudSwitch @bind-Value="ReadOnly" Color="Color.Primary">ReadOnly</MudSwitch> <MudSwitch @bind-Value="Hover" Color="Color.Primary">Hover</MudSwitch> <MudSwitch @bind-Value="Ripple" Color="Color.Primary">Ripple</MudSwitch> <MudSwitch @bind-Value="Dense" Color="Color.Primary">Dense</MudSwitch> <MudSwitch @bind-Value="Disabled" Color="Color.Primary">Disabled</MudSwitch> <MudSwitch @bind-Value="ExpandOnClick" Color="Color.Primary">ExpandOnClick</MudSwitch> <MudSwitch @bind-Value="ExpandOnDoubleClick" Color="Color.Primary">ExpandOnDoubleClick</MudSwitch> </MudStack>
@code { public bool ReadOnly = true; public bool Hover = true; public bool Ripple; public bool Dense; public bool Disabled; public bool ExpandOnClick = true; public bool ExpandOnDoubleClick; }
Icons
The icons and their color can be changed individually per item via Icon
and IconColor
. This example uses a custom
ExpandButtonIcon
and shows how to apply an alternative icon for expanded subtrees via the IconExpanded
property.
All Mail
Drafts
Orders
Categories
Social
Updates
Forums
Spam
Trash
<MudTreeView T="string"> <MudTreeViewItem Value='"All Mail"' Icon="@Icons.Material.Filled.Email" /> <MudTreeViewItem Value='"Drafts"' Icon="@Icons.Material.Filled.Drafts" /> <MudTreeViewItem Value='"Orders"' Icon="@Icons.Material.Filled.Label" IconColor="Color.Secondary" /> <MudTreeViewItem Value='"Categories"' Icon="@Icons.Custom.Uncategorized.Folder" IconExpanded="@Icons.Custom.Uncategorized.FolderOpen" IconColor="Color.Info" ExpandButtonIcon="@Icons.Material.Filled.ArrowRight" ExpandButtonIconColor="Color.Primary"> <MudTreeViewItem Value='"Social"' Icon="@Icons.Material.Filled.Group" /> <MudTreeViewItem Value='"Updates"' Icon="@Icons.Material.Filled.Info" IconColor="Color.Tertiary" /> <MudTreeViewItem Value='"Forums"' Icon="@Icons.Material.Filled.QuestionAnswer" /> <MudTreeViewItem Value='"Spam"' Icon="@Icons.Material.Filled.LocalOffer" /> </MudTreeViewItem> <MudTreeViewItem Value='"Trash"' Icon="@Icons.Material.Filled.Delete" /> </MudTreeView>
Single Selection
If you set SelectionMode
to SelectionMode.SingleSelection
you can select a single value from the entire tree.
SelectionMode.ToggleSelection
is similar, except that it allows to deselect a previously selected value by clicking on it again.
You can use @bind-SelectedValue
on the <MudTreeView>
to get updates about the selected value or to influence
the selected value like you can do in this example with the chip set.
The color of the selected item can be changed with Color
property.
config
launch.json
tasks.json
images
logo.png
<MudPaper Width="300px" Elevation="0"> <MudTreeView Hover ReadOnly="" @bind-SelectedValue="SelectedValue" SelectionMode=""> <MudTreeViewItem Value="@("config")" Expanded Icon="@Icons.Custom.Uncategorized.Folder" IconExpanded="@Icons.Custom.Uncategorized.FolderOpen"> <MudTreeViewItem Value='"launch.json"' Icon="@Icons.Custom.FileFormats.FileCode" /> <MudTreeViewItem Value='"tasks.json"' Icon="@Icons.Custom.FileFormats.FileCode" /> </MudTreeViewItem> <MudTreeViewItem Value="@("images")" Icon="@Icons.Custom.Uncategorized.Folder" IconExpanded="@Icons.Custom.Uncategorized.FolderOpen"> <MudTreeViewItem Value="@("logo.png")" Icon="@Icons.Custom.FileFormats.FileImage" /> </MudTreeViewItem> </MudTreeView> </MudPaper> <MudStack Row Justify="Justify.Center" Style="width: 100%" Wrap="Wrap.Wrap"> <MudRadioGroup @bind-Value="SelectionMode"> <MudRadio Value="SelectionMode.SingleSelection" Color="Color.Primary">SingleSelection</MudRadio> <MudRadio Value="SelectionMode.ToggleSelection" Color="Color.Primary">ToggleSelection</MudRadio> </MudRadioGroup> <MudChipSet T="string" @bind-SelectedValue="SelectedValue" Color="Color.Primary" Variant="Variant.Text"> <MudChip Text="config"/> <MudChip Text="launch.json"/> <MudChip Text="tasks.json"/> <MudChip Text="images"/> <MudChip Text="logo.png"/> </MudChipSet> <MudSwitch @bind-Value="ReadOnly" Color="Color.Primary">ReadOnly</MudSwitch> </MudStack>
@code { public string SelectedValue = "tasks.json"; public bool ReadOnly = false; public SelectionMode SelectionMode = SelectionMode.SingleSelection; }